package com.ziyia.roomtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.List;
import java.util.PrimitiveIterator;

public class MainActivity extends AppCompatActivity {
    private UserDao userDao;
    private BookDao bookDao;
    private User u1 = new User("年少有为", 20);
    private User u2 = new User("Room", 29);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userDao = AppDatabase.getAppDatabase(this).userDao();
        bookDao = AppDatabase.getAppDatabase(this).bookDao();
    }

    public void insert(View view) {
        new Thread(() -> {
//            u1.setId(userDao.insertUser(u1));
//            u2.setId(userDao.insertUser(u2));
            userDao.insertUser(u2);
        }).start();
    }

    public void select(View view) {
        new Thread(() -> {
            List<User> users = userDao.loadAllUsers();
            for (User user : users) {
                Log.d("for-user", user.toString());

            }
        }).start();
    }

    public void delete(View view) {
        new Thread(() -> {
            userDao.deleteUserByName("年少有为");
        }).start();
    }

    public void update(View view) {
        new Thread( () -> {
            userDao.updateUser(new User(8L, "年少有为", 17));
        }).start();
    }

    public void insertBook(View view) {
        new Thread(() -> {
            bookDao.insertBook(new Book("Java核心技术", 870, "aaa"));
        }).start();
    }


    public void selectBook(View view) {
        new Thread(
                () -> {
                    List<Book> books = bookDao.loadAllBook();
                    for (Book book : books) {
                        Log.d("Book", book.toString());
                    }
                }
        ).start();
    }
}